﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class AJAX_AjaxHandler : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Reference.MobileServiceClient client = new Reference.MobileServiceClient();
            GovReference.InfoCategoryClient GovClient = new GovReference.InfoCategoryClient();
            string token = string.Empty;
            try
            {
                token = Session[Consts.UserToken].ToString();
            }
            catch (Exception ex)
            {
                Response.Redirect("Login.aspx");
            }
            if (string.IsNullOrEmpty(token))
            {
                Response.Redirect("Login.aspx");
            }
            string operationCode = Request.Form["operationCode"];
            string title = Request.Form["title"];
            int pageIndex = Convert.ToInt32(Request.Form["pageIndex"]);
            int pageSize = Convert.ToInt32(Request.Form["pageSize"]);
            int recordCount = 0;
            string rtnXML = string.Empty;
            switch (operationCode)
            { 
                //通知通告
                case "notice":
                    
                    //查询条件
                    string whereClause = string.Empty;
                    if (string.IsNullOrEmpty(title))
                        whereClause = " 1=1 ";
                    else
                    {
                        whereClause = string.Format(" {0} LIKE N'%{1}%'", "Title", title);
                    }
                    string orderColumn = "  ReadStatus,SendTime ";
                    string orderDirection = " DESC ";
                    rtnXML = client.GetReceivedInformList(token, whereClause, pageIndex, pageSize, ref recordCount, orderColumn, orderDirection);
                    Response.ContentType = "text/xml";
                    Response.Write(rtnXML);
                    break;
                   
                    //通知流转
                case "noticeDistribute":
                    string delieverID = Request.Form["deliverID"];
                    string nusers = Request.Form["users"];
                    string rContent = Request.Form["rContent"];
                    if (nusers.Length > 0)
                    {
                        nusers = nusers.Substring(0, nusers.Length - 1);

                        client.InforDistribute(token, delieverID, nusers, rContent, false); 
                    }
                    Response.Write("ok");  
                    break;


                //专送件
                case "express":
                    //查询条件
                    StringBuilder Clause = new StringBuilder();
                    if (string.IsNullOrEmpty(title))
                        Clause.Append(" 1=1 ");
                    else
                    {
                        Clause.Append(string.Format(" {0} LIKE N'%{1}%'", "Title", title));
                    } 
                    rtnXML = client.GetReceiveSendList(token, Clause, pageSize, pageIndex, ref recordCount);
                    Response.ContentType = "text/xml";
                    Response.Write(Server.HtmlDecode(rtnXML));
                    break;
                //专送件
                case "expressSend":

                    rtnXML = client.GetRSendList(token, title, " ProcessDate desc", pageSize, pageIndex, ref recordCount);
                    Response.ContentType = "text/xml";
                    Response.Write(Server.HtmlDecode(rtnXML));
                    break;
                //政务简报
                case "GovNews":
                    //查询条件
                    rtnXML = GovClient.getJournal(token, pageSize, pageIndex, title, ref recordCount);
                    Response.ContentType = "text/xml";
                    Response.Write(Server.HtmlDecode(rtnXML));
                    break;

                //政务信息
                case "GovMessage":
                    //查询条件
                    rtnXML = GovClient.GetCategoryInfoList(pageSize, pageIndex, title, ref recordCount);
                    Response.ContentType = "text/xml";
                    Response.Write(Server.HtmlDecode(rtnXML));
                    break;
                //部门信息
                case "DeptMessage":
                    //查询条件
                    rtnXML = GovClient.GetDepartInfoList(token, pageSize, pageIndex, title, ref recordCount);
                    Response.ContentType = "text/xml";
                    Response.Write(Server.HtmlDecode(rtnXML));
                    break;
                //二级回复
                case "Reply":
                    string expressId = Request.Form["expressId"];
                    string replyId = Request.Form["replyId"];
                    string msg = Request.Form["msg"];
                    System.Diagnostics.EventLog.WriteEntry("Application", msg);
                    client.insertTwoFeedBack(token, new Guid(expressId), new Guid(replyId), msg, true, "", "");
                    Response.Write("ok");
                    break;
                case "SearchUser":
                    string UserName = Request.Form["userName"].ToString();
                    rtnXML = client.GetSearchName(UserName);
                    Response.ContentType = "text/xml";
                    Response.Write(Server.HtmlDecode(rtnXML));
                    break;
                case "RsSend":
                    string content = Request.Form["content"];
                    string selectUsers = Request.Form["users"];
                    if (selectUsers.Length > 0)
                    {
                        selectUsers = selectUsers.Substring(0, selectUsers.Length - 1);
                        client.SpecialSend(token, title, content, selectUsers);
                    }
                    Response.Write("ok");
                    break;
                case "RsDistrbute":
                    Guid infoid = new Guid(Request.Form["infoId"].ToString());
                    string contentD = Request.Form["content"];
                    string selectUsersD = Request.Form["users"];
                    if (selectUsersD.Length > 0)
                    {
                        selectUsersD = selectUsersD.Substring(0, selectUsersD.Length - 1);
                       
                        client.RSDistribute(token, infoid, contentD, selectUsersD, false);
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
